#include "EventLoop.h"

#include <thread>

namespace Engine2D
{
    void EventLoop::Start(std::function<void(EventLoop *)> fn, int fps)
    {
        m_oldTime = std::chrono::steady_clock::now();
        while (CurrentStatus() == Status::Start)
        {
            for (auto && event : m_eventQueue) {
                event();
            }
            m_eventQueue.clear();
            fn(this);
            auto now = std::chrono::steady_clock::now();
            auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_oldTime).count();
            m_oldTime = now;
            if (duration < 1000 / fps) {
                std::this_thread::sleep_for(std::chrono::milliseconds(1000 / fps - duration));
            }
        }
    }
    
} // namespace Engine2D

